﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Input;

using AtletIS.DomainModel.DTOs;
using AtletIS.DomainModel.Entities.Main;
using AtletIS.Service.Services;
using AtletIS.UI.Commands;
using AtletIS.UI.Messaging;
using AtletIS.UI.ViewModels.Base;

namespace AtletIS.UI.ViewModels
{
    public class StartListViewModel : MediatorViewModelBase<ViewModelMessages>
    {
        
        private const int PAGE_SIZE = 8;
        
        #region Private properties

        private DisciplineList _activeDisciplineList;
        private List<StartListDto> _startList;
        
        private int _totalCount = 0;
        private bool _hasPages = true;
        private int _page = 1;
        private int _pageCount = 1;

        #endregion

        #region Public properties

        public ICommand NextPageCommand { get; set; }
        public ICommand PrevPageCommand { get; set; }

        public DisciplineList ActiveDisciplineList
        {
            get { return _activeDisciplineList; }
            set
            {
                _activeDisciplineList = value;
                OnPropertyChanged("ActiveDisciplineList");

                this.Page = 1;
                this.GetStartList(this.ActiveDisciplineList, this.HasPages);
            }
        }

        public List<StartListDto> StartList 
        {
            get { return _startList; }
            set
            {
                _startList = value;
                OnPropertyChanged("StartList");
            }
        }

        public int TotalCount
        {
            get { return _totalCount; }
            set
            {
                _totalCount = value;
                OnPropertyChanged("TotalCount");
            }
        }

        public bool HasPages
        {
            get { return _hasPages; }
            set
            {
                _hasPages = value;
                OnPropertyChanged("HasPages");

                this.GetStartList(this.ActiveDisciplineList, this.HasPages);
            }
        }

        public int Page
        {
            get { return _page; }
            set
            {
                _page = value;
                OnPropertyChanged("Page");
            }
        }

        public int PageCount
        {
            get { return _pageCount; }
            set
            {
                _pageCount = value;
                OnPropertyChanged("PageCount");
            }
        }

        #endregion

        #region Constructor

        public StartListViewModel(DisciplineList disciplineList, bool hasPages)
        {
            if (disciplineList != null)
            {
                this.ActiveDisciplineList = disciplineList;
                this.HasPages = hasPages;
            }

            this.SubscribeToMessages();
            this.NextPageCommand = new RelayCommand(new Action<object>(GetNextPage));
            this.PrevPageCommand = new RelayCommand(new Action<object>(GetPrevPage));
        }

        #endregion

        #region Messaging

        private void SubscribeToMessages()
        {
            base.Mediator.Register(ViewModelMessages.ActiveDisciplineListChanged, param => OnActiveDisciplineListChanged(param as DisciplineList));
            base.Mediator.Register(ViewModelMessages.HasPagesChanged, param => OnHasPagesChanged((bool)param));
        }

        void OnActiveDisciplineListChanged(DisciplineList disciplineList)
        {
            this.ActiveDisciplineList = disciplineList;
        }

        void OnHasPagesChanged(bool hasPages)
        {
            this.HasPages = hasPages;
        }

        #endregion

        #region Private methods

        private void GetNextPage(object obj)
        {
            if (this.Page < this.PageCount)
            {
                this.Page += 1;
                this.GetStartList(this.ActiveDisciplineList, true);
            }
        }

        private void GetPrevPage(object obj)
        {
            if (this.Page > 1)
            {
                this.Page -= 1;
                this.GetStartList(this.ActiveDisciplineList, true);
            }
        }

        private void GetStartList(DisciplineList disciplineList, bool hasPages)
        {
            if (disciplineList != null)
            {
                int totalCount;
                
                using (var service = new StartListService())
                {
                    if (hasPages)
                    {
                        this.StartList = service.GetStartListCollection(disciplineList, out totalCount, this.Page, PAGE_SIZE);
                        this.TotalCount = totalCount;
                        this.PageCount = this.TotalCount % PAGE_SIZE == 0 ? this.TotalCount / PAGE_SIZE : this.TotalCount / PAGE_SIZE + 1;
                        if (this.PageCount == 0)
                        {
                            this.PageCount += 1;
                        }
                    }
                    else
                    {
                        this.StartList = service.GetStartListCollection(disciplineList, out totalCount);
                        this.TotalCount = totalCount;
                    }
                }
            }
        }

        #endregion

    }
}
